<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目号" prop="orderNo">
        <el-input
          v-model="queryParams.orderNo"
          placeholder="请输入项目号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="正常" value="0" />
          <el-option label="异常" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item label="进度" prop="progress">
        <el-input
          v-model="queryParams.progress"
          placeholder="请输入进度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类目" prop="category">
        <el-input
          v-model="queryParams.category"
          placeholder="请输入类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后人员" prop="afterSalePerson">
        <el-input
          v-model="queryParams.afterSalePerson"
          placeholder="请输入售后人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['operations:afterSaleFollow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['operations:afterSaleFollow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['operations:afterSaleFollow:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['operations:afterSaleFollow:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="afterSaleFollowList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="项目号" align="center" prop="orderNo" width="180" />
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'danger'">
            {{ scope.row.status === '0' ? '正常' : '异常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="progress" width="120" show-overflow-tooltip />
      <el-table-column label="类目" align="center" prop="category" width="120" show-overflow-tooltip />
      <el-table-column label="最终交付日期" align="center" prop="finalDeliveryDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finalDeliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交付日期" align="center" prop="deliveryDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接单价格" align="center" prop="orderPrice" width="120" />
      <el-table-column label="最终价格" align="center" prop="finalPrice" width="120" />
      <el-table-column label="差价" align="center" prop="priceDifference" width="120" />
      <el-table-column label="首款" align="center" prop="firstPayment" width="120" />
      <el-table-column label="中款" align="center" prop="midPayment" width="120" />
      <el-table-column label="尾款" align="center" prop="finalPayment" width="120" />
      <el-table-column label="售后人员" align="center" prop="afterSalePerson" width="120" />
      <el-table-column label="备注" align="center" prop="remark" width="200" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['operations:afterSaleFollow:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['operations:afterSaleFollow:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改售后跟单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目号" prop="orderNo">
              <el-select v-model="form.orderNo" placeholder="请选择项目号" filterable clearable style="width: 100%">
                <el-option
                  v-for="item in orderOptions"
                  :key="item.id"
                  :label="item.orderNo"
                  :value="item.orderNo">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 100%">
                <el-option label="正常" value="0" />
                <el-option label="异常" value="1" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="类目" prop="category">
              <el-input v-model="form.category" placeholder="请输入类目" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="售后人员" prop="afterSalePerson">
              <el-input v-model="form.afterSalePerson" placeholder="自动填充" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="进度" prop="progress">
              <el-input v-model="form.progress" type="textarea" :rows="3" placeholder="请输入进度" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="最终交付日期" prop="finalDeliveryDate">
              <el-date-picker clearable
                v-model="form.finalDeliveryDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择最终交付日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中期交付周期" prop="midDeliveryCycle">
              <el-input v-model="form.midDeliveryCycle" placeholder="请输入中期交付周期" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="中期时间" prop="midTime">
              <el-date-picker clearable
                v-model="form.midTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择中期时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="交付日期" prop="deliveryDate">
              <el-date-picker clearable
                v-model="form.deliveryDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择交付日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="接单价格" prop="orderPrice">
              <el-input-number v-model="form.orderPrice" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入接单价格" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最终价格" prop="finalPrice">
              <el-input-number v-model="form.finalPrice" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入最终价格" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="包单价格" prop="packagePrice">
              <el-input-number v-model="form.packagePrice" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入包单价格" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="差价" prop="priceDifference">
              <el-input-number v-model="form.priceDifference" :step="100" :precision="2" controls-position="right" placeholder="请输入差价" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="首款" prop="firstPayment">
              <el-input-number v-model="form.firstPayment" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入首款" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="首款日期" prop="firstPaymentDate">
              <el-date-picker clearable
                v-model="form.firstPaymentDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择首款日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="补首款" prop="supplementFirstPayment">
              <el-input-number v-model="form.supplementFirstPayment" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入补首款" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="补首款日期" prop="supplementFirstPaymentDate">
              <el-date-picker clearable
                v-model="form.supplementFirstPaymentDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择补首款日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="中款" prop="midPayment">
              <el-input-number v-model="form.midPayment" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入中款" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="中款日期" prop="midPaymentDate">
              <el-date-picker clearable
                v-model="form.midPaymentDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择中款日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="尾款" prop="finalPayment">
              <el-input-number v-model="form.finalPayment" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入尾款" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="尾款日期" prop="finalPaymentDate">
              <el-date-picker clearable
                v-model="form.finalPaymentDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择尾款日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="硬件费" prop="hardwareFee">
              <el-input-number v-model="form.hardwareFee" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入硬件费" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="硬件费用户日期" prop="hardwareFeeUserDate">
              <el-date-picker clearable
                v-model="form.hardwareFeeUserDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择硬件费用户日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="其他费用" prop="otherFee">
              <el-input-number v-model="form.otherFee" :step="100" :min="0" :precision="2" controls-position="right" placeholder="请输入其他费用" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他费用用户日期" prop="otherFeeUserDate">
              <el-date-picker clearable
                v-model="form.otherFeeUserDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择其他费用用户日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="技术信息" prop="technicalInfo">
              <el-input v-model="form.technicalInfo" type="textarea" :rows="3" placeholder="请输入技术信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAfterSaleFollow, getAfterSaleFollow, delAfterSaleFollow, addAfterSaleFollow, updateAfterSaleFollow } from "@/api/operations/afterSaleFollow";
import { getAfterSaleOrderOptions } from "@/api/dashboard/orders";

export default {
  name: "AfterSaleFollow",
  data() {
    return {
      // 订单选项列表
      orderOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 售后跟单表格数据
      afterSaleFollowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        status: null,
        progress: null,
        category: null,
        afterSalePerson: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNo: [
          { required: true, message: "项目号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.loadOrderOptions();
  },
  methods: {
    /** 加载订单选项 */
    loadOrderOptions() {
      getAfterSaleOrderOptions().then(response => {
        this.orderOptions = response.data || [];
      }).catch(error => {
        console.error('加载订单选项失败:', error);
        this.orderOptions = [];
      });
    },
    /** 查询售后跟单列表 */
    getList() {
      this.loading = true;
      listAfterSaleFollow(this.queryParams).then(response => {
        this.afterSaleFollowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gradOrdersId: null,
        orderNo: null,
        status: "0",
        progress: null,
        category: null,
        finalDeliveryDate: null,
        midDeliveryCycle: null,
        midTime: null,
        deliveryDate: null,
        orderPrice: null,
        finalPrice: null,
        packagePrice: null,
        priceDifference: null,
        firstPayment: null,
        firstPaymentDate: null,
        supplementFirstPayment: null,
        supplementFirstPaymentDate: null,
        midPayment: null,
        midPaymentDate: null,
        finalPayment: null,
        finalPaymentDate: null,
        hardwareFee: null,
        hardwareFeeUserDate: null,
        otherFee: null,
        otherFeeUserDate: null,
        technicalInfo: null,
        afterSalePerson: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      // 自动填充售后人员为当前登录用户
      const username = this.$store.getters.name;
      this.form.afterSalePerson = username;
      this.open = true;
      this.title = "添加售后跟单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAfterSaleFollow(id).then(response => {
        this.form = response.data;
        // 如果售后人员为空，自动填充为当前登录用户
        if (!this.form.afterSalePerson) {
          const username = this.$store.getters.name;
          this.form.afterSalePerson = username;
        }
        this.open = true;
        this.title = "修改售后跟单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAfterSaleFollow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAfterSaleFollow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除售后跟单编号为"' + ids + '"的数据项？').then(function() {
        return delAfterSaleFollow(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('operations/afterSaleFollow/export', {
        ...this.queryParams
      }, `afterSaleFollow_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

